//
//  BPRequestTool.m
//  BPRequestTool_Example
//
//  Created by SiYugui on 2020/4/29.
//  Copyright © 2020年 xuhongji. All rights reserved.
//

#import "BPRequestTool.h"

@implementation BPRequestTool

+ (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters headers:(NSDictionary<NSString *,NSString *> *)headers requestSerializer:(BPRequestSerializerType)requestSerializer responseSerializer:(BPResponseSerializerType)responseSerializer success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nullable))failure{
    AFHTTPSessionManager *manager = [self managerWithURL:URLString requestType:requestSerializer responseType:responseSerializer];
    NSURLSessionDataTask *dataTask = [manager GET:URLString parameters:parameters headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    return dataTask;
}

+ (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters headers:(NSDictionary<NSString *,NSString *> *)headers requestSerializer:(BPRequestSerializerType)requestSerializer responseSerializer:(BPResponseSerializerType)responseSerializer success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nullable))failure{
    AFHTTPSessionManager *manager = [self managerWithURL:URLString requestType:requestSerializer responseType:responseSerializer];
    NSURLSessionDataTask *dataTask = [manager POST:URLString parameters:parameters headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    return dataTask;
}

+(NSURLSessionDataTask *)PUT:(NSString *)URLString parameters:(id)parameters headers:(NSDictionary<NSString *,NSString *> *)headers requestSerializer:(BPRequestSerializerType)requestSerializer responseSerializer:(BPResponseSerializerType)responseSerializer success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nullable))failure{
    AFHTTPSessionManager *manager = [self managerWithURL:URLString requestType:requestSerializer responseType:responseSerializer];
    NSURLSessionDataTask *dataTask = [manager PUT:URLString parameters:parameters headers:headers success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    return dataTask;
}

+ (NSURLSessionDataTask *)DELETE:(NSString *)URLString parameters:(id)parameters headers:(NSDictionary<NSString *,NSString *> *)headers requestSerializer:(BPRequestSerializerType)requestSerializer responseSerializer:(BPResponseSerializerType)responseSerializer success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nullable))failure{
    AFHTTPSessionManager *manager = [self managerWithURL:URLString requestType:requestSerializer responseType:responseSerializer];
    manager.requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD",@"DELETE" ,nil];
    NSURLSessionDataTask *dataTask = [manager DELETE:URLString parameters:parameters headers:headers success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    return dataTask;
}

+(NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters requestSerializer:(BPRequestSerializerType)requestSerializer headers:(NSDictionary<NSString *,NSString *> *)headers responseSerializer:(BPResponseSerializerType)responseSerializer constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> _Nullable))block progress:(void (^)(NSProgress * _Nonnull))progress success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nullable))failure{
    AFHTTPSessionManager *manager = [self managerWithURL:URLString requestType:requestSerializer responseType:responseSerializer];
    NSURLSessionDataTask *dataTask = [manager POST:URLString parameters:parameters headers:headers constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if (block) {
            block(formData);
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    return dataTask;;
}


#pragma mark - cancel all request
+ (void)cancelAllRequests{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    [manager.operationQueue cancelAllOperations];
}


#pragma mark - private
/**
 请求类型构造
 
 @param requestSerializerType 类型
 @return AFHTTPRequestSerializer
 */
+ (AFHTTPRequestSerializer *)requestSerializerWithSerializerType:(BPRequestSerializerType)requestSerializerType{
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    switch (requestSerializerType) {
            case BPRequestSerializerDefault:
        {
            serializer = [AFHTTPRequestSerializer serializer];
            return serializer;
        }
            break;
            case BPRequestSerializerJson:
        {
            serializer = [AFJSONRequestSerializer serializer];
            return serializer;
        }
            break;
        default:
            break;
    }
    
    return serializer;
}

/**
 解析类型构造
 
 @param responseSerializerType 类型
 @return AFHTTPResponseSerializer
 */
+ (AFHTTPResponseSerializer *)resposnseSerializerWithSerializerType:(BPResponseSerializerType)responseSerializerType{
    AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
    
    switch (responseSerializerType) {
            case BPResponseSerializerTypeDefault:
        {
            serializer = [AFHTTPResponseSerializer serializer];
            return serializer;
        }
            break;
            case BPResponseSerializerTypeJson:
        {
            serializer = [AFJSONResponseSerializer serializer];
            return serializer;
        }
            break;
            case BPResponseSerializerTypeImage:
        {
            serializer = [AFImageResponseSerializer serializer];
            return serializer;
        }
            break;
            case BPResponseSerializerTypeData:
        {
            serializer = [AFHTTPResponseSerializer serializer];
            return serializer;
        }
            break;
        default:
            break;
    }
    
    return serializer;
}

+ (AFHTTPSessionManager *)managerWithURL:(NSString*)urlString requestType:(BPRequestSerializerType)requsetSerializer responseType:(BPResponseSerializerType)responseSerializer{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    manager.requestSerializer = [self requestSerializerWithSerializerType:requsetSerializer];
    manager.responseSerializer = [self resposnseSerializerWithSerializerType:responseSerializer];
    manager.requestSerializer.timeoutInterval = BPRequestTimeoutInterval;
    return manager;
}

@end
